﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Zimmerreservierung.Model;
using System.Data.SQLite;

namespace Zimmerreservierung.Forms
{
    public partial class FormRechnung : Form
    {
        private SQLiteConnection connection;
        private List<Reservierung> reservierungen;

        public FormRechnung(List<Reservierung> reservierungen, SQLiteConnection connection)
        {
            InitializeComponent();
            this.connection = connection;
            this.btnSave.DialogResult = DialogResult.OK;
            this.btnCancel.DialogResult = DialogResult.Cancel;
            this.reservierungen = reservierungen;

            var bindinglist = new BindingList<Reservierung>(reservierungen);
            var source = new BindingSource(bindinglist, null);
            dataGridView1.DataSource = source;
            dataGridView1.RowHeadersVisible = false;
            for (int i = 0; i < dataGridView1.ColumnCount; i++)
            {
                dataGridView1.Columns[i].Visible = false;
            }

            dataGridView1.Columns[3].Visible = true;
            dataGridView1.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridView1.Columns[4].Visible = true;
            dataGridView1.Columns[4].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridView1.Columns[6].Visible = true;
            dataGridView1.Columns[6].AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;

            dataGridView1.AutoSize = true;

            tbGesamt.Text = reservierungen.Sum(x => x.Zimmerpreis).ToString();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            // abbrechen
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // speichern
            try
            {
                int kundenId = 0;
                if (dataGridView1.Rows.Count == 1)
                {
                    kundenId = reservierungen[0].Kunde.Id;
                }
                else
                {
                    FormKundeAuswaehlen auswahl = new FormKundeAuswaehlen( reservierungen.Select(x => x.Kunde).ToList<Kunde>());
                    auswahl.ShowDialog();
                    kundenId = auswahl.SelectedKundenId;                    
                }

                connection.Open();
                using (SQLiteCommand command = new SQLiteCommand(connection))
                {
                    

                    command.CommandText = "INSERT INTO Rechnung ( ID, KundenId, Gesamtpreis, Datum ) "
                                                   + "VALUES (NULL, '" + kundenId + "', '" + tbGesamt.Text.Trim() + "', '" + DateTime.Now + "' );";
                    command.ExecuteNonQuery();

                    command.CommandText = "SELECT last_insert_rowid();";
                    int id = Convert.ToInt32(command.ExecuteScalar());

                    // update reservierungen
                    foreach (Reservierung res in reservierungen)
                    {
                        command.CommandText = "UPDATE Reservierung SET RechnungsId = '" + id + "' WHERE Id = '" + res.Id + "';";
                        command.ExecuteNonQuery();
                    }

                }
                connection.Close();
                
                MessageBox.Show("Rechnung erfolgreich gespeichert.");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }


        }
    }
}
